package com.jwgf.smarowindingmachine_unity.ui;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.UserLogin;
import com.jwgf.objectbox.UserLogin_;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import io.objectbox.Box;

public class LoginFragment extends BaseFragment {

	private static String userId;
	private static int writeLevel = 0;
	private static int readLevel = 0;

	private View warning;
	private Spinner userIdEdit;
	private EditText passwordEdit;

	private ArrayAdapter<String> adapter;
	private ArrayList<String> list = new ArrayList<String>();

	private Runnable callback;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_login, container, false);
		warning = v.findViewById(R.id.warning_layout);
		userIdEdit = (Spinner) v.findViewById(R.id.user_id_value);
		passwordEdit = (EditText) v.findViewById(R.id.password_value);
//		passwordEdit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
//		passwordEdit.setRawInputType(Configuration.KEYBOARD_QWERTY);

		passwordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				Box<UserLogin> box = ObjectBox.get().boxFor(UserLogin.class);
				UserLogin user = box.query().equal(UserLogin_.user, (String) userIdEdit.getSelectedItem()).build().findUnique();
				if (user.user.equals("Factory")) {
					String time = "0";
					int year = 0, month = 0, day = 0;
					year = Calendar.getInstance().get(Calendar.YEAR);
					month = Calendar.getInstance().get(Calendar.MONTH) + 1;
					day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
					time = String.valueOf((year + month + day + 1980) * day);
					if (passwordEdit.getText().toString().equals(time)) {
						setUserId(user.user);
						setWriteLevel(user.writeLevel);
						setReadLevel(user.readLevel);
						FragmentManager fm = getFragmentManager();
						FragmentTransaction ft = fm.beginTransaction();
						ft.hide(LoginFragment.this);
						ft.commitNow();
						new Handler(Looper.getMainLooper()).post(callback);

						passwordEdit.setText("");
						warning.setVisibility(View.INVISIBLE);
					} else {
						warning.setVisibility(View.VISIBLE);
					}
				} else {
					if (passwordEdit.getText().toString().equals(user.password)) {
						setUserId(user.user);
						setWriteLevel(user.writeLevel);
						setReadLevel(user.writeLevel);
						FragmentManager fm = getFragmentManager();
						FragmentTransaction ft = fm.beginTransaction();
						ft.hide(LoginFragment.this);
						ft.commitNow();
						new Handler(Looper.getMainLooper()).post(callback);

						passwordEdit.setText("");
						warning.setVisibility(View.INVISIBLE);
					} else {
						warning.setVisibility(View.VISIBLE);
					}
				}
				return false;
			}
		});


		Box<UserLogin> user = ObjectBox.get().boxFor(UserLogin.class);
		if (user.getAll().size() == 0) {
			UserLogin administrator = new UserLogin();
			administrator.user = "Administrator";
			administrator.password = "0000";
			administrator.writeLevel = 3;
			administrator.readLevel = 3;
			user.put(administrator);

			UserLogin maintainer = new UserLogin();
			maintainer.user = "Maintainer";
			maintainer.password = "0000";
			maintainer.writeLevel = 3;
			maintainer.readLevel = 3;
			user.put(maintainer);

			UserLogin operator = new UserLogin();
			operator.user = "Operator";
			operator.password = "0000";
			operator.writeLevel = 1;
			operator.readLevel = 1;
			user.put(operator);

			UserLogin factory = new UserLogin();
			factory.user = "Factory";
			factory.writeLevel = 4;
			factory.readLevel = 4;
			user.put(factory);
		}

		userIdEdit.setAdapter(adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_dropdown_item_1line, getData()));
		return v;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden) {
			getData();
			adapter.notifyDataSetChanged();
		}
	}

	private List<String> getData() {
		list.clear();
		Box<UserLogin> box = ObjectBox.get().boxFor(UserLogin.class);
		List<UserLogin> listUser = box.getAll();
		for (UserLogin user : listUser) {
			list.add(user.user);
		}
		return list;
	}

	public void registerRunnable(Runnable r) {
		if (r != null) {
			callback = r;
		}
	}

	public static String getUserId() {
		return userId;
	}

	public static void setUserId(String userId) {
		LoginFragment.userId = userId;
	}

	public static int getWriteLevel() {
		return writeLevel;
	}

	public static void setWriteLevel(int writeLevel) {
		LoginFragment.writeLevel = writeLevel;
	}

	public static int getReadLevel() {
		return readLevel;
	}

	public static void setReadLevel(int readLevel) {
		LoginFragment.readLevel = readLevel;
	}

	public static void loginOut() {
		if (getUserId() != null) {
			setUserId(null);
			setWriteLevel(0);
			setReadLevel(0);
		}
	}
}
